package com.sangu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangu.constants.SystemConstant;
import com.sangu.entity.Article;
import com.sangu.mapper.ArticleMapper;
import com.sangu.service.ArticleService;
import com.sangu.utils.BeanUtils;
import com.sangu.vo.HotArticleVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Override
    public List<HotArticleVO> hotArticleList() {
        //查询热门文章，过滤掉草稿和已删除文章
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        //正式文章
        wrapper.eq(Article::getStatus, SystemConstant.ARTICLE_STATUS_NORMAL);
        //浏览量排序
        wrapper.orderByDesc(Article::getViewCount);
        //分页查询，最多查询10条
        Page<Article> page = new Page<>(1,0);
        page(page,wrapper);
        List<Article> articleList = page.getRecords();
        //Bean拷贝
        List<HotArticleVO> vos = BeanUtils.copyBeanList(articleList, HotArticleVO.class);

        return vos;
    }
}
